package com.whz.generic.utils;

import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * url编解码工具
 *
 * @author whz
 */
@Slf4j
@UtilityClass
public class UrlCodecUtils {

    @SneakyThrows
    public String urlEncode(final String value, final String charset) {
        return URLEncoder.encode(value, charset);
    }

    /**
     * UTF-8 URL编码字符串
     *
     * @param value 输入字符串
     * @return UTF-8 编码后的字符串
     */
    @SneakyThrows
    public String urlEncode(final String value) {
        return URLEncoder.encode(value, StandardCharsets.UTF_8.name());
    }

    @SneakyThrows
    public String urlDecode(final String value, final String charset) {
        return URLDecoder.decode(value, charset);
    }

    /**
     * UTF-8 url解码字符串
     *
     * @param value 编码的字符串
     * @return UTF-8 解码的字符串
     */
    @SneakyThrows
    public String urlDecode(final String value) {
        return URLDecoder.decode(value, StandardCharsets.UTF_8.name());
    }
}